package com.tianhua.javs.core.parse;

import com.tianhua.javs.client.bean.JavsShellVarBean;
import com.tianhua.javs.core.constant.TagConstants;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * date: 2022/2/10
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class JavsShellParseService {

    /**
     * 将一行javs脚本转换为JavsShellVar模型
     * @param rowContent
     * @return
     */
    public List<JavsShellVarBean> parseJavsShellRow(String rowContent){
        String [] shellArr = rowContent.split(TagConstants.TWO_COLON);
        String invokeContent = shellArr[1];

        List<JavsShellVarBean> varModelList = new ArrayList<>();
        if(invokeContent.contains(TagConstants.EQ_GT)){
            String [] multRowArr = invokeContent.split(TagConstants.EQ_GT);
            for (String multRow : multRowArr){
                varModelList.addAll(splitByOnePoint(multRow));
            }
        }else {
            varModelList = splitByOnePoint(invokeContent);
        }

        return varModelList;
    }

    /**
     * 通过.来分割生成javs变量模型
     * @param invokeContent
     * @return
     */
    private List<JavsShellVarBean> splitByOnePoint(String invokeContent){
        List<JavsShellVarBean> varModelList = new ArrayList<>();
        String [] keyWardArr = invokeContent.split(TagConstants.ONE_POINT);
        for (int i = 0;i < keyWardArr.length;i ++){
            if(keyWardArr[i].contains("=") && !keyWardArr[i].contains("=>")){
                String [] eqArr = keyWardArr[0].split("=");
                varModelList.add(JavsShellVarBean.ofInstance(eqArr[0],true));
                varModelList.add(JavsShellVarBean.ofInstance(eqArr[1],false));
            }else {
                varModelList.add(JavsShellVarBean.ofInstance(keyWardArr[i],false));
            }
        }
        return varModelList;
    }

}
